window.onload = function () {
    changePattern();
    var myinfo = getSession("data");
    var avatar = document.getElementById("avatar");
    var name = document.getElementById("name");
    if (myinfo != null) {
        avatar.src = "http://www.rushmc.top/" + myinfo.photo;
        name.innerHTML = myinfo.name;
    }
    createPage(1, "http://www.rushmc.top/api/dynamicState/select/page", "")
    var drop = document.getElementById("drop");
    var dynamicChoise = document.getElementById("dynamic-choise");
    drop.onclick = function (event) {
        if (!getSession("data")) {
            return
        }
        dynamicChoise.innerHTML = event.target.innerHTML;
        if (dynamicChoise.innerHTML == "本人动态") {
            createPage(1, "http://www.rushmc.top/api/dynamicState/select", "删除动态", myinfo.id)
        } else {
            createPage(1, "http://www.rushmc.top/api/dynamicState/select/page", "")
        }
    }
    var photoWrapper = document.getElementById("photo-wrapper");
    var files = document.getElementById("files");
    var flag = 0;
    var filesarr = new Array;
    files.onchange = function (event) {
        flag = 1;
        for (let i = 0; i < event.target.files.length; i++) {
            filesarr.push(event.target.files[i]);
        }
        showPreview(event.target, photoWrapper);
        var cancelFile = document.getElementById("cancelFile");
        cancelFile.onclick = function () {
            var photo_S_wrapper = document.getElementById("S_wrapper");
            photoWrapper.removeChild(photo_S_wrapper);
            event.target.value = "";
            filesarr.length = 0;
            flag = 0;
        }
    }
    // 发布动态
    var senddynamic = document.getElementsByClassName("senddynamic");
    var postWrapper = document.getElementById("postWrapper");
    if (!getSession("data")) {
        postWrapper.previousElementSibling.innerHTML = "登录后发表动态";
        postWrapper.innerHTML = "";
    }
    senddynamic[0].onblur = function () {
        senddynamic[3].innerHTML = "发布动态";
        if (senddynamic[0].innerHTML == "") {
            senddynamic[0].innerHTML = "填入标题";
        }
    }
    senddynamic[1].onblur = function () {
        senddynamic[3].innerHTML = "发布动态";
        if (senddynamic[1].innerHTML == "") {
            senddynamic[1].innerHTML = "发表你的动态";
        }
    }
    if (getCookie("dynamic")) {
        var dynamic = getCookie("dynamic");
        dynamic = window.JSON.parse(dynamic);
    }
    if (dynamic == null) {
        senddynamic[2].innerHTML = "保存动态"
    } else {
        senddynamic[2].innerHTML = "取消保存";
        senddynamic[0].innerHTML = dynamic.dynamicTitle;
        senddynamic[1].innerHTML = dynamic.dynamicContent
    }
    senddynamic[2].onclick = function () {
        if (senddynamic[0].innerHTML == "填入标题") {
            senddynamic[2].innerHTML = "请填入标题再保存";
            return
        }
        if (senddynamic[1].innerHTML == "发表你的动态") {
            senddynamic[2].innerHTML = "请填入内容再保存";
            return
        }
        if (senddynamic[2].innerHTML == "保存动态") {
            console.log(filesarr);
            setCookie("dynamic", {
                dynamicTitle: senddynamic[0].innerHTML,
                dynamicContent: senddynamic[1].innerHTML

            }, 7)
            senddynamic[2].innerHTML = "取消保存";
        } else {
            deleteCookie("dynamic", 1, -1);
            senddynamic[2].innerHTML = "保存动态"
        }
    }
    senddynamic[3].onclick = function () {
        if (senddynamic[0].innerHTML == "填入标题") {
            senddynamic[3].innerHTML = "请填入标题再发布";
            return
        }
        if (senddynamic[1].innerHTML == "发表你的动态") {
            senddynamic[3].innerHTML = "请填入内容再发布";
            return
        }
        ajax({
            type: "post",
            url: "http://www.rushmc.top/api/dynamicState/insert",
            data: {
                userId: myinfo.id,
                content: senddynamic[1].innerHTML,
                title: senddynamic[0].innerHTML,
                hasPicture: flag,
                files: filesarr
            },
            success: function () {
                senddynamic[3].innerHTML = "发布成功！";
                var photo_S_wrapper = document.getElementById("S_wrapper");
                senddynamic[0].innerHTML = "填入标题";
                senddynamic[1].innerHTML = "发表你的动态";
                senddynamic[3].innerHTML = "发布动态";
                if (getCookie("dynamic")) {
                    deleteCookie("dynamic");
                }
                if (photo_S_wrapper) {
                    photoWrapper.removeChild(photo_S_wrapper);
                }
                filesarr.length = 0;
                flag = 0;
                if (dynamicChoise.innerHTML == "本人动态") {
                    createPage(1, "http://www.rushmc.top/api/dynamicState/select", "删除动态", myinfo.id)
                } else {
                    createPage(1, "http://www.rushmc.top/api/dynamicState/select/page", "")
                }
            }
        })
    }
    var up = document.getElementById("up");
    moveup(up);
    search();
}